import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import '../res/app_design.dart';

GestureTapCallback _close = () => Get.back();

typedef WidgetWrap = Widget Function(Widget child);
WidgetWrap _defPop = (child) {
  return PopScope(canPop: false, child: child);
};

const Color mainColor = Color(0xff652D13);

WidgetWrap _defDialog = (child) {
  return Dialog(
    insetPadding: EdgeInsets.zero,
    backgroundColor: const Color(0xffF8F6E7),
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.w))),
    child: child,
  );
};

class BtnModel {
  String title;
  GestureTapCallback? callback;

  BtnModel(this.title, {this.callback});

  factory BtnModel.confirm({String? title, GestureTapCallback? callback}) =>
      BtnModel(title ?? "confirm".tr, callback: callback);
  factory BtnModel.cancel({String? title, GestureTapCallback? callback}) =>
      BtnModel(title ?? "cancel".tr, callback: callback);
}

abstract class CommDialog extends StatelessWidget {
  final WidgetWrap popScope;
  final WidgetWrap defDialog;

  CommDialog({super.key, WidgetWrap? popScope, WidgetWrap? defDialog})
      : popScope = popScope ?? _defPop,
        defDialog = defDialog ?? _defDialog;

  @override
  Widget build(BuildContext context) {
    return popScope.call(defDialog.call(Container(
      width: 595.w,
      constraints: BoxConstraints(
        minHeight: 162.w,
      ),
      padding: EdgeInsets.all(27.w),
      child: body(context),
    )));
  }

  Widget body(BuildContext context);

  void show({
    bool barrierDismissible = true,
    Color? barrierColor,
    bool useSafeArea = true,
    GlobalKey<NavigatorState>? navigatorKey,
    Object? arguments,
    Duration? transitionDuration,
    Curve? transitionCurve,
    String? name,
    RouteSettings? routeSettings,
  }) {
    Get.dialog(this,
        barrierDismissible: barrierDismissible,
        barrierColor: barrierColor,
        useSafeArea: useSafeArea,
        navigatorKey: navigatorKey,
        arguments: arguments,
        transitionDuration: transitionDuration,
        transitionCurve: transitionCurve,
        name: name,
        routeSettings: routeSettings);
  }
}

class MessageDialog extends CommDialog {
  final BtnModel? cancel;
  final BtnModel confirm;
  final String? title;
  final String? content;
  final Widget? contentWgt;

  MessageDialog(
      {this.title, this.content, this.cancel, this.contentWgt, BtnModel? confirm, super.key})
      : confirm = confirm ?? BtnModel.confirm();

  @override
  Widget body(BuildContext context) {
    return DefaultTextStyle(
      style: AppStyle.app_text.copyWith(fontSize: AppDimen.sp_25, color: mainColor),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (title != null) genTitleWidget(title!),
          if (content != null) contentWidget(content!),
          contentWgt ?? Container(),
          bottomWidget(cancel, confirm),
        ],
      ),
    );
  }



  Widget contentWidget(String content) {
    return Container(
        constraints: BoxConstraints(
          minHeight: 250.w,
        ),
        child: Text(content).center());
  }

  Widget bottomWidget(BtnModel? cancel, BtnModel confirm) {
    List<Widget> children = [];
    if (cancel != null) {
      children.add(
          genBtn(cancel.title,onTap:cancel.callback ?? _close,confirm: false ,width: 209.w).marginOnly(right: 15.w));
    }
    children.add(
      genBtn(confirm.title,onTap:confirm.callback ?? _close ));

    return Row(
      children: children,
    ).paddingOnly(top: 25.w, bottom: 25.w);
  }
}

Widget genTitleWidget(String title) {
  return Text(
    title,
    style: TextStyle(fontSize: 30.sp),
  );
}

Widget genBtn(String title, {double? height, double? width, bool confirm = true,GestureTapCallback? onTap}) {
  var txCor = confirm ? AppColors.white : mainColor;
  var bgCor = confirm ? const Color(0xff4A230A) : null;
  var border = confirm ?  null:Border.all(color: const Color(0xff4A230A));
  var content =  Container(
      height: height ?? 65.w,
      width: width,
      decoration:
          BoxDecoration(color: bgCor,
              border: border,
              borderRadius: BorderRadius.all(Radius.circular(10.w))),
      child: Text(
        title,
        style: AppStyle.app_text_20_bold.copyWith(fontSize: AppDimen.sp_22, color: txCor),
      ).center()).onTap(onTap);

  return confirm ? content.expanded():content;

}
